package com.server.questionbank.snframe.mapper.Course;

import com.server.questionbank.snframe.domain.QuestionBank.Question;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TQuestionMapper {
    // 根据题目id查询问题
    @Select("SELECT * FROM T_question WHERE exercises_id = #{exercisesId} ORDER BY display_order asc")
    public List<Question> getQuestionByExercisesId(@Param("exercisesId") Long exercisesId);

    // 添加问题
    @Insert("INSERT INTO T_question ( " +
            "            exercises_id, question_title, question_type, " +
            "            display_order, answer_options, " +
            "            answer, analysis, score " +
            "        ) VALUES ( " +
            "            #{question.exercisesId}, #{question.questionTitle}, #{question.questionType}, " +
            "            #{question.displayOrder}, #{question.answerOptions}, " +
            "            #{question.answer}, #{question.analysis}, #{question.score} " +
            "        )")
    public Long addQuestion(@Param("question") Question question);

    // 更新问题
    @Update("UPDATE T_question SET " +
            "            question_title = #{question.questionTitle}, " +
            "            question_type = #{question.questionType}, " +
            "            display_order = #{question.displayOrder}, " +
            "            answer_options = #{question.answerOptions}, " +
            "            answer = #{question.answer}, " +
            "            analysis = #{question.analysis}, " +
            "            score = #{question.score} " +
            "        WHERE rec_id = #{question.recId}")
    public Long updateQuestion(@Param("question") Question question);

    // 删除问题
    @Delete("DELETE FROM T_question WHERE rec_id = #{questionId}")
    public Long deleteQuestion(@Param("questionId") Long questionId);


    // 根据问题的类型查询
    @Select("<script>" +
            "SELECT q.*, e.title  " +
            "FROM T_question q LEFT JOIN T_exercises e ON q.exercises_id = e.exercises_id " +
            "WHERE q.exercises_id IN " +
            "<foreach item='id' collection='exercisesIds' open='(' separator=',' close=')'>#{id}</foreach>" +
            "<if test='questionType != null'> AND q.question_type = #{questionType}</if>" +
            "</script>")
    List<Question> findByExercisesIdsAndType(
            @Param("exercisesIds") List<Long> exercisesIds,
            @Param("questionType") String questionType
    );

}
